﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dark_Core_Library.NetworkModules.HTTP;

namespace Dark_Core_Library.UniModules.Models
{
    /// <summary>
    /// 模型助手类
    /// </summary>
    public static class ModelHelper
    {
        /// <summary>
        /// 将对象转换为操作结果对象
        /// </summary>
        /// <typeparam name="TS">操作源类型</typeparam>
        /// <typeparam name="TR">操作返回值类型</typeparam>
        /// <param name="sender">操作源对象</param>
        /// <param name="result">操作结果对象</param>
        /// <param name="msg">操作消息</param>
        /// <param name="isSuccess">是否成功</param>
        /// <returns></returns>
        public static UniOperationResult<TS, TR> ToOperationResult<TS, TR>(this TS sender, TR result, string? msg = null, bool isSuccess = true) => UniOperationResult<TS, TR>.CreateInstance(sender, result, msg ?? (isSuccess ? "操作成功" : "操作失败"), isSuccess);

        /// <summary>
        /// 创建操作模式聚合对象
        /// </summary>
        /// <typeparam name="TSender">操作源</typeparam>
        /// <typeparam name="TMode">操作模式</typeparam>
        /// <typeparam name="TValue">操作值</typeparam>
        /// <param name="sender">操作源对象</param>
        /// <param name="mode">操作模式</param>
        /// <param name="value">操作值</param>
        /// <returns></returns>
        public static UniOperationModeAggregation<TSender, TMode, TValue> CreateOperationModeAggregation<TSender, TMode,
            TValue>(this TSender sender, TMode mode, TValue value)
            where TSender:class
        where TMode:struct,Enum
            =>
            UniOperationModeAggregation<TSender, TMode, TValue>.CreateInstance(sender, value, mode);


        /// <summary>
        /// 提取分页参数
        /// </summary>
        /// <param name="request">通用请求</param>
        /// <param name="valueFieldName">值字段名称</param>
        /// <param name="tagFieldName">标签字段名称</param>
        /// <param name="pageNumFieldName">分页序号字段名称</param>
        /// <param name="pageSizeFieldName">分页大小字段名称</param>
        /// <returns></returns>
        public static UniPaginationParam<string> ExtractPagingParams(this UniHttpRequest request, string valueFieldName = "value", string tagFieldName = "tag",string pageNumFieldName = "pageNum",string pageSizeFieldName ="pageSize")
        {
            request.TryGetValue(pageNumFieldName, out int pageNum, 0);
            request.TryGetValue(pageSizeFieldName, out int pageSize, 99999);
            request.TryGetValue(tagFieldName, out string tag, string.Empty);
            request.TryGetValue(valueFieldName, out string value, string.Empty);
            return new UniPaginationParam<string>(pageNum,pageSize,value,tag);
        }

    }
}
